package com.han.neo4jjava.neo4j.person;

import com.han.neo4jjava.core.person.Person;
import com.han.neo4jjava.core.person.PersonRepo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

import static java.util.stream.Collectors.toList;

@Component
@RequiredArgsConstructor
public class Neo4jPersonRepoImpl implements PersonRepo {

    private final PersonDao personDao;

    @Override
    public List<Person> findAll() {
        List<PersonModel> personModels = personDao.findAll();
        return personModels.stream().map(PersonModel::convert2Person).collect(toList());
    }

    @Override
    public List<Person> queryAllByName(String name) {
        return personDao.findAllByName(name).stream().map(PersonModel::convert2Person).collect(toList());
    }

    @Override
    public void drop() {
        personDao.drop();
    }

    @Override
    public void save(Person person) {
        personDao.save(PersonModel.convert2PersonModel(person));
    }

    @Override
    public void saveAll(List<Person> persons) {
        List<PersonModel> personModels = persons.stream().map(PersonModel::convert2PersonModel).collect(toList());
        personDao.saveAll(personModels);
    }

}
